package com.mandao.uploadservice;

import com.mandao.uploadservice.config.StorageProperties;
import com.mandao.uploadservice.service.StorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author snail
 * 文件上传服务
 */
@Slf4j
@SpringBootApplication
@EnableConfigurationProperties(StorageProperties.class)
public class UploadServiceApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(UploadServiceApplication.class, args);
        ServerProperties bean = run.getBean(ServerProperties.class);
        Integer port = bean.getPort();
        String path = bean.getServlet().getPath();

        InetAddress address = null;
        String hostAddress = "";
        try {
            address = InetAddress.getLocalHost();
            if (null != address) {
                hostAddress = address.getHostAddress();
            }
        } catch (UnknownHostException e) {
            log.info("Get current server IP exception【{}】", e);
        }
        log.info("-------------- start\tsuccess: " + hostAddress + ":" + port + path + "--------------");
    }

    @Bean
    CommandLineRunner init(StorageService storageService) {
        return (args) -> {
            storageService.deleteAll();
            storageService.init();
        };
    }
}
